require 'View/UIBase/UIBase'

UIFirstPayGift = UIBase.New(PanelResNames.UIFirstPayGift,UIBase)
local PayType=
{
    firstPay=0,
    accumulatePay=1,
}
local payFirstID=30001
local accumulateID=11004
function UIFirstPayGift.Init(func, id)
    UIFirstPayGift.activeId = id;
    UIBase.Init(UIFirstPayGift,func);
end

-- 创建回调
function UIFirstPayGift:OnCreate()
    --AddUIPackageDepend(PanelResNames.UIFirstPayGift,"FUI/BusinessAtlas")
    self.drawWrapper = GoWrapper.New()
    self.countDownNum = 0;

    self.typeController=self:GetController("Type")
    self.typeController.selectedIndex=0
    self.lihui = self:GetChild("lihui");
    self.fristTitle = self:GetChild("fristTitle");
    self.secondTitle = self:GetChild("secondTitle");
    self.Graph_hua01 = self:GetChild("Graph_hua01");
    self.payController = self:GetController("pay");
    self.itemList = self:SetDynamicList(self:GetChild("AwardList"),self.updateItem, nil, true, true)
    self.itemList1 = self:SetDynamicList(self:GetChild("AwardList1"),self.updateItem, nil, true, true)
    self.coin = self:GetChild("CoinLb");
    self.cashAdd = self.coin:GetChild("add");
    local addCtrl = self.coin:GetController("add");
    addCtrl.selectedIndex = 1;
    self.isShowController=self:GetController("IsShow")
    self.btnType1=self:GetChild("btnType1")
    self:RegisterOnClick(self.btnType1,self.ClickBtnFirstPay)

    self.btnType2=self:GetChild("btnType2")
    self:RegisterOnClick(self.btnType2,self.ClickBtnAccumulate)

    self.btnBuy=self:GetChild("btnBuy")
    self:addEvent();
end
function UIFirstPayGift:ClickBtnFirstPay()
    if self.payType==PayType.firstPay then
        return
    end
    self.payType=PayType.firstPay
    self.typeController.selectedIndex=0
    self.activeId=payFirstID
    self:Refresh()
end
function UIFirstPayGift:ClickBtnAccumulate()
    if self.payType==PayType.accumulatePay then
        return
    end
    self.payType=PayType.accumulatePay
    self.typeController.selectedIndex=1
    self.activeId=accumulateID
    self:Refresh()
end

-- 显示
function UIFirstPayGift:OnEnable()
    if self.activeId == nil then
        self:OnBack();
    else
        self.payType=-1;
        local temp=logicMgr.CostManager.disposeFirstPayGiftData(payFirstID)
        local isShowPayFirst=(logicMgr.CostManager.getFunMenuById(payFirstID)~=nil)
        self.isShowController.selectedIndex=2
        if not isShowPayFirst then
            self.isShowController.selectedIndex=1
            self:ClickBtnAccumulate()
        else
            local firstPayFlag=RedDotsFirstPayGiftHandler.getFlag(nil,1)== Config.RedDotsFlagState.Needful
            local accumulateFlag=RedDotsFirstPayGiftHandler.getFlag(nil,2)== Config.RedDotsFlagState.Needful
            if accumulateFlag and not firstPayFlag then
                self:ClickBtnAccumulate()
            else
                self:ClickBtnFirstPay()
            end
        end
    end
    logicMgr.HeroManager.setGetNewHeroShow(true);
    UIRedDots.addViewListen(PanelNames.UIFirstPayGift, "UIFirstPayGift" .. "btnType1", self.btnType1,Config.RedDotsType.FirstPayGift,1,1);
    UIRedDots.addViewListen(PanelNames.UIFirstPayGift, "UIFirstPayGift" .. "btnType2", self.btnType2,Config.RedDotsType.FirstPayGift,1,2);
end

function UIFirstPayGift:addEvent()
    self:RegisterOnClick(self:GetChild("bgClose"),self.OnClose);
    self:RegisterOnClick(self:GetChild("Backbtn"),self.OnClose);
    self:RegisterOnClick(self:GetChild("btnGo"),self.OnGo);
    --self:RegisterOnClick(self:GetChild("btnBuy"),self.OnBuy);
    self:RegisterEvent(Config.EventType.UICostUpdate,self.Refresh);
    self:RegisterEvent(Config.EventType.Fresh_Active,self.Refresh)
    self.cashAdd.onClick:Add(self.onClickCoinAdd);
end

function UIFirstPayGift:removeEvent()
    self:UnRegisterEvent(Config.EventType.Fresh_Active, self.Refresh);
    self:UnRegisterEvent(Config.EventType.UICostUpdate, self.Refresh);
    self.cashAdd.onClick:Remove(self.onClickCoinAdd);
end

function UIFirstPayGift.onClickCoinAdd()
    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Cost, true, UIFirstPayGift.cashAdd) then
        UIFirstPayGift:OnClose();
        logicMgr.CostManager.gotoCostById(UIFirstPayGift.modelTableData.f_GotoStoreId);
    end
end

function UIFirstPayGift:Refresh()
    self:UpdateData();
    self:RefreshCoin()
end
function UIFirstPayGift:RefreshCoin()
    self.coin:GetChild("icon").icon = "ui://ItemAtlas/"..CurrencyEnum.GOLD;
    self.coin:GetChild("title").text = getNumString(logicMgr.ItemManager.getCurrencyCountByModelId(1));
end
function UIFirstPayGift:UpdateData()
    if self.payType==PayType.firstPay then
        self:updateFirstPayData()
    elseif self.payType ==PayType.accumulatePay then
        self:updateAccumulatePayData()
    end
end
function UIFirstPayGift:updateFirstPayData()
    self.serverData, self.tableData, self.modelTableData = logicMgr.CostManager.getCostData(self.activeId);
    self.fristTitle.text = getLanguage(self.modelTableData.f_HeroFristName);
    self.secondTitle.text = getLanguage(self.modelTableData.f_HeroSeconName);
    if self.serverData.receiveState == 0 then
        self.payController.selectedIndex = 0;
    elseif self.serverData.receiveState == 1 then
        self.payController.selectedIndex = 1;
    elseif self.serverData.receiveState == 2 then
        self.payController.selectedIndex = 3;
    else
        self.payController.selectedIndex = 2;
    end
    self.rewardTable = decodeJsonStrToTabel(self.modelTableData.f_Reward);
    self.itemList.numItems = #self.rewardTable;
    local path = HeroManager.GetHeroDrawingPath(self.modelTableData.f_Hero);
    self:SetModel(path,0.5)
    self.btnBuy.onClick:Clear()
    self.btnBuy.onClick:Add(function ()
        logicMgr.CostManager.getRewardById(self.activeId, nil, self.onBuySuccess, self);
    end)
end
function UIFirstPayGift:SetModel(path,timeScale)
    resMgr:LoadModelPrefab(path, function(go)
        if(self.RoleLihui~=nil)then
            destroyImmediate(self.RoleLihui);
            self.RoleLihui = nil
        end
        self.RoleLihui = newObject(go);
        self.RoleLihui.name = path;
        --self.RoleLihui.name = self.modelTableData.f_Hero;
        Util.AddAction(self.RoleLihui,"idle1",true,timeScale);
        self.RoleLihui:AddComponent(TCanvas);
        self.drawWrapper:SetWrapTarget(self.RoleLihui,false);
        Util.Lua_SetLocalPositionXYZ(self.RoleLihui.transform,0,0,0)
        Util.Lua_SetLocalScaleXYZ(self.RoleLihui.transform,1.1,1.1,1.1)
        self.lihui:SetNativeObject(self.drawWrapper);
    end)
    resMgr:LoadModelPrefab("UIEff/efc_hua01", function(go)
        if self.shuiPaoDrawWrapper == nil then
            self.shuiPaoDrawWrapper = GoWrapper.New();
        end
        if (self.shuiPaoObj~= nil) then
            destroyImmediate(self.shuiPaoObj);
        end
        self.shuiPaoObj = newObject(go);
        self.shuiPaoObj:AddComponent(TCanvas);
        self.shuiPaoObj:AddComponent(TChangeSortingOrder)
        self.shuiPaoDrawWrapper:SetWrapTarget(self.shuiPaoObj, false)
        Util.Lua_SetLocalPositionXYZ(self.shuiPaoObj.transform, 0, 0, 0)
        self.Graph_hua01:SetNativeObject(self.shuiPaoDrawWrapper);
    end)
end
function UIFirstPayGift:updateAccumulatePayData()
    self.accumulateState,self.accumulateTableData,self.accumulateServerData=logicMgr.ActiveManager.getActiveData(self.activeId);
    if self.accumulateState==nil then
        return
    end
    self.payController.selectedIndex=self.accumulateState
    self.rewardTable=decodeJsonStrToTabel(self.accumulateTableData.f_Reward)
    self.accumulateTitles=decodeJsonStrToTabel(self.accumulateTableData.f_TitleAward)
    self.itemList1.numItems=#self.rewardTable
    local index=self.accumulateServerData.dayNum-1-1
    index=Mathf.Clamp(index,0,self.itemList1.numItems-1)
    self.itemList1:ScrollToView(index, true, true)

    self.btnBuy.onClick:Clear()
    self.btnBuy.onClick:Add(function ()
        ActivityHandler.sendReqActivityDrawRewardMessage({self.activeId});
    end)
    local path = HeroManager.GetHeroDrawingPath(self.accumulateTableData.f_Hero);
    self:SetModel(path,1)
    self:GetChild("EightTitle1").text=string.format(getLanguage("EightTitle1"), self.accumulateServerData.totalPay,98)
    self.fristTitle.text = getLanguage(self.accumulateTableData.f_HeroFristName);
    self.secondTitle.text = getLanguage(self.accumulateTableData.f_HeroSeconName);

end
function UIFirstPayGift:updateItem(index, itemObj)
    if self.payType==PayType.firstPay then
        self:updateFirstPayItem(index,itemObj)
    elseif self.payType==PayType.accumulatePay then
        self:updateAccumulatePayItem(index,itemObj)
    end
end
function UIFirstPayGift:updateFirstPayItem(index,itemObj)
    local itemReward = self.rewardTable[index];
    local serverData = self.serverData.receiveList[index];
    if itemReward == nil or #itemReward == 0 then
        itemObj.visible = false;
    end
    itemObj.visible = true;
    local title = itemObj:GetChild("title");
    local itemList = itemObj:GetChild("itemList");
    local typeController = itemObj:GetController("IsGet");
    local NumberController = itemObj:GetController("Number");
    title.text = getLanguage(self.modelTableData.f_TitleAward[index], nil, "");
    if serverData == 1 then
        typeController.selectedIndex = 1;
    elseif serverData == 2 then
        typeController.selectedIndex = 2;
    else
        typeController.selectedIndex = 0;
    end
    NumberController.selectedIndex = #itemReward - 1;
    logicMgr.CostManager.RefreshItemList(itemList, itemReward);
    itemList:ResizeToFit();
end
function UIFirstPayGift:updateAccumulatePayItem(index,itemObj)
    local itemReward = self.rewardTable[index];
    local itemList = itemObj:GetChild("itemList");
    local getController = itemObj:GetController("IsGet");
    local rewardedSet=self.accumulateServerData.rewardedSet
    local day=self.accumulateServerData.dayNum
    if index<=day then
        if index<=#rewardedSet then
            getController.selectedIndex = 2;
        else
            getController.selectedIndex=1
        end
    else
        getController.selectedIndex=0
    end
    local title = itemObj:GetChild("title");
    title.text=getLanguage(self.accumulateTitles[index])
    logicMgr.CostManager.RefreshItemList(itemList, itemReward)
    itemList:ResizeToFit();

end
--- 跳转
function UIFirstPayGift:OnGo()
    self:OnClose();
    logicMgr.CostManager.gotoCostById(self.modelTableData.f_GotoStoreId);
end

--- 购买
--function UIFirstPayGift:OnBuy()
--    logicMgr.CostManager.getRewardById(self.activeId, nil, self.onBuySuccess, self);
--end

-- 购买成功
function UIFirstPayGift:onBuySuccess()
    FireEvent(Config.EventType.Fresh_FuncMenuData);
    FireEvent(Config.EventType.Fresh_FirstPayGiftRedDot);
    --self:OnClose();
end

-- 关闭
function UIFirstPayGift:OnClose()
    self:OnBack();
    logicMgr.HeroManager.setGetNewHeroShow();
end

-- 隐藏
function UIFirstPayGift:OnDisable()
    UIRedDots.removeViewEvent(PanelNames.UIFirstPayGift)
end

-- 卸载
function UIFirstPayGift:OnDestroy()
    self:removeEvent();
    if self.RoleLihui then
        destroyImmediate(self.RoleLihui)
        self.RoleLihui = nil
    end
    destroy(self.shuiPaoObj)
    self.shuiPaoObj = nil
end

return UIFirstPayGift